package com.neu.flashmall.product.service.impl;

import com.neu.flashmall.product.entity.PmsAttr;
import com.neu.flashmall.product.entity.PmsProductAttrValue;
import com.neu.flashmall.product.entity.PmsSkuSaleAttrValue;
import com.neu.flashmall.product.mapper.PmsAttrMapper;
import com.neu.flashmall.product.mapper.PmsSkuSaleAttrValueMapper;
import com.neu.flashmall.product.service.PmsAttrService;
import com.neu.flashmall.product.service.PmsProductAttrValueService;
import com.neu.flashmall.product.vo.AttrFilterVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collector;

@Service
public class PmsAttrServiceImpl implements PmsAttrService {

    @Resource
    private PmsAttrMapper pmsAttrMapper;

    @Resource
    private PmsProductAttrValueService pmsProductAttrValueService;

    @Resource
    private PmsSkuSaleAttrValueMapper pmsSkuSaleAttrValueMapper;

    @Override
    public List<PmsAttr> selectAll(){
        return this.pmsAttrMapper.selectAll();
    }

    @Override
    public String changeSearchType(Long id, Integer now) {
        PmsAttr pmsAttr = new PmsAttr();
        pmsAttr.setId(id);
        if(now == 0){
            pmsAttr.setSearchType(1);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将检索状态设置为可检索";
            }
        }else if(now == 1){
            pmsAttr.setSearchType(0);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将检索状态设置为不可检索";
            }
        }
        return "检索状态代码异常或者数据库异常";
    }

    @Override
    public String changeValueType(Long id, Integer now) {
        PmsAttr pmsAttr = new PmsAttr();
        pmsAttr.setId(id);
        if(now == 0){
            pmsAttr.setValueType(1);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将属性值状态设置为多值";
            }
        }else if(now == 1){
            pmsAttr.setValueType(0);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将属性值状态设置为单值";
            }
        }
        return "属性值状态代码异常或者数据库异常";
    }

    @Override
    public String changeAttrType(Long id, Integer now) {
        PmsAttr pmsAttr = new PmsAttr();
        pmsAttr.setId(id);
        if(now == 0){
            pmsAttr.setAttrType(1);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将属性类型设置为基本属性";
            }
        }else if(now == 1){
            pmsAttr.setAttrType(2);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将属性类型设置为即是销售属性也是基本属性";
            }
        }else if(now == 2){
            if(pmsProductAttrValueService.canDeleteAttr(id) > 0){
                pmsAttr.setAttrType(1);
                if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                    return "由于有SPU使用该属性，所以不能设置为销售属性，已经将其设置为基本属性";
                }
            }else{
                pmsAttr.setAttrType(0);
                if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                    return "已经将属性类型设置为销售属性";
                }
            }

        }
        return "属性类型代码异常或者数据库异常";
    }

    @Override
    public String changeEnable(Long id, Integer now) {
        PmsAttr pmsAttr = new PmsAttr();
        pmsAttr.setId(id);
        if(now == 0){
            pmsAttr.setEnable(1L);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经启用该属性";
            }
        }else if(now == 1){
            if(pmsProductAttrValueService.canDeleteAttr(id) > 0){
                return "该属性已经被SPU所使用，无法禁用，需要先在SPU中取消该属性的使用";
            }
            pmsAttr.setEnable(0L);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经禁用该属性";
            }
        }
        return "属性状态代码异常或者数据库异常";
    }

    @Override
    public String changeShow(Long id, Integer now) {
        PmsAttr pmsAttr = new PmsAttr();
        pmsAttr.setId(id);
        if(now == 0){
            pmsAttr.setShowDesc(1);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将该属性的展示状态设置为展示";
            }
        }else if(now == 1){
            pmsAttr.setShowDesc(0);
            if(pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr) == 1){
                return "已经将该属性的展示状态设置为不展示";
            }
        }
        return "展示状态代码异常或者数据库异常";
    }

    @Override
    public int canDeleteAttrGroup(Long id) {
        return this.pmsAttrMapper.checkCanDeleteGroup(id);
    }

    @Override
    public List<PmsAttr> getAllAttrByGroup(Integer groupId) {
        return pmsAttrMapper.selectByGroup(groupId);
    }

    @Override
    public String addAttr(PmsAttr pmsAttr) {
        if(checkDuplicate(pmsAttr.getGroup(),null,pmsAttr.getAttrName()) != 0){
            return "该属性组下已经存在同名的属性了";
        }
        int result = this.pmsAttrMapper.insertSelective(pmsAttr);
        if(result == 1) return "添加成功";
        return "数据库异常";
    }

    @Override
    public String updateAttr(PmsAttr pmsAttr) {
        if(checkDuplicate(pmsAttr.getGroup(),pmsAttr.getId(),pmsAttr.getAttrName()) != 0){
            return "该属性组下已经存在同名的属性了";
        }
        int result = this.pmsAttrMapper.updateByPrimaryKeySelective(pmsAttr);
        if(result == 1) return "更新成功";
        return "数据库异常";
    }

    @Override
    @Transactional
    public String deleteAttr(Long id) {
        if (pmsProductAttrValueService.canDeleteAttr(id) > 0) {
            return "该属性已被SPU所使用，无法删除。";
        }
        int result = this.pmsAttrMapper.deleteByPrimaryKey(id);
        if (result == 1) return "删除成功";
        return "数据库异常";
    }

    @Override
    public int deleteAllByGroup(Integer group) {
        return pmsAttrMapper.deleteAllByGroup(group);
    }

    public List<PmsAttr> getByCategoryId(Long categoryId){
        return this.pmsAttrMapper.getByCategoryIdForSpu(categoryId);
    }

    @Override
    public List<PmsAttr> getSkuAttrByCategoryId(Long categoryId) {
        return pmsAttrMapper.getByCategoryIdForSku(categoryId);
    }

    @Override
    public AttrFilterVo getAttrFilterVo(Long categoryId) {
        AttrFilterVo attrFilterVo = new AttrFilterVo();
        List<PmsAttr> allAttr = pmsAttrMapper.getByCategoryIdForFilter(categoryId);
        List<AttrFilterVo.Item> data = new ArrayList<>();
        for(PmsAttr attr : allAttr){
            List<PmsProductAttrValue> spuAttr = pmsProductAttrValueService.selectByAttrId(attr.getId());
            Set<String> set = new HashSet<>();
            Iterator<PmsProductAttrValue> iterator = spuAttr.iterator();
            while (iterator.hasNext()){
                PmsProductAttrValue pmsProductAttrValue = iterator.next();
                if(set.contains(pmsProductAttrValue.getAttrValue())){
                    iterator.remove();
                }else{
                    set.add(pmsProductAttrValue.getAttrValue());
                }
            }
            AttrFilterVo.Item one = new AttrFilterVo.Item();
            one.setAttrDetail(attr);
            List<AttrFilterVo.Attr> valueList = new ArrayList<>();
            for(PmsProductAttrValue pmsProductAttrValue : spuAttr) {
                AttrFilterVo.Attr newAttr = new AttrFilterVo.Attr();
                newAttr.setId(pmsProductAttrValue.getId());
                newAttr.setValue(pmsProductAttrValue.getAttrValue());
                valueList.add(newAttr);
            }

            List<PmsSkuSaleAttrValue> skuAttr = pmsSkuSaleAttrValueMapper.selectByAttrId(attr.getId());
            Set<String> set2 = new HashSet<>();
            for(AttrFilterVo.Attr a : valueList){
                set2.add(a.getValue());
            }
            Iterator<PmsSkuSaleAttrValue> iterator1 = skuAttr.iterator();
            while(iterator1.hasNext()){
                PmsSkuSaleAttrValue pmsSkuSaleAttrValue = iterator1.next();
                if(set2.contains(pmsSkuSaleAttrValue.getAttrValue())){
                    iterator1.remove();
                }else{
                    set2.add(pmsSkuSaleAttrValue.getAttrValue());
                }
            }
            for(PmsSkuSaleAttrValue pmsSkuSaleAttrValue : skuAttr){
                AttrFilterVo.Attr newAttr = new AttrFilterVo.Attr();
                newAttr.setId(pmsSkuSaleAttrValue.getId());
                newAttr.setValue(pmsSkuSaleAttrValue.getAttrValue());
                valueList.add(newAttr);
            }
            AttrFilterVo.Attr newAttr = new AttrFilterVo.Attr();
            newAttr.setId(-1L);
            newAttr.setValue("全部");
            valueList.add(0,newAttr);
            one.setValueList(valueList);
            data.add(one);
        }
        attrFilterVo.setData(data);
        return attrFilterVo;
    }

    private int checkDuplicate(Integer group,Long id,String attrName){
        if(id == null){
            id = -1L;
        }
        List<PmsAttr> same = this.pmsAttrMapper.selectDuplicate(group,id,attrName);
        return same.size();
    }
}
